<template>
	<view>
		<view class="search-box">
		  <my-search @click="gotoSearch"></my-search>
		</view>
		<!-- 轮播 -->
		<swiper circular indicator-dots autoplay :interval="3000" class="swiper">
			<swiper-item v-for="item in swiperList" :key="item.goods_id">
				<navigator :url="`/subpkg/goods-detail/goods-detail?goods_id=${item.goods_id}`" class="swiper-item">
					<image :src="item.image_src"></image>
				</navigator>
			</swiper-item>
		</swiper>
		<!-- 分类导航 -->
		<view class="nav-list">
			<view class="nav-item" v-for="(item,index) in navList" :key="index" @click="navClickHandler(item)">
				<image :src="item.image_src"></image>
			</view>
		</view>
		<!-- 楼层区域 -->
		<view class="floor-list">
			<view class="floor-item" v-for="(item,index) in floorList" :key="index">
				<image :src="item.floor_title.image_src" class="floor-title"></image>
				<view class="floor-img-box">
					<view class="left-img-box">
						<image :src="item.product_list[0].image_src"
							:style="{width: item.product_list[0].image_width + 'rpx'}" mode="widthFix"></image>
					</view>
					<view class="right-img-box">
						<block class="right-img-item" v-for="(e,i) in item.product_list" :key="i">
							<!-- <navigator :url="`/subpkg/goods-list/goods-list?${e.navigator_url}`"> -->
							<image :src="e.image_src" v-if="i!== 0" :style="{width:e.image_width + 'rpx'}"
								mode="widthFix" @click="toListHandler(e)"></image>
							<!-- </navigator> -->
						</block>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				swiperList: [],
				navList: [],
				floorList: []
			};
		},
		onLoad() {
			this.getSwiperList()
			this.getNavList()
			this.getFloorList()
		},
		methods: {
			// 跳转搜索
			gotoSearch() {
			  uni.navigateTo({
			    url: '/subpkg/search/search'
			  })
			},
			toListHandler(item) {
				console.log(item.navigator_url)
				uni.navigateTo({
					url: '/subpkg/goods-list/goods-list?' + item.navigator_url
				});
			},
			async getSwiperList() {
				const {
					data: {
						message,
						meta
					}
				} = await uni.$http.get('/api/public/v1/home/swiperdata')
				// if (meta.status !== 200) uni.showToast({
				// 	title:'数据加载失败',
				// 	duration:1500
				// })
				if (meta.status !== 200) return uni.showMsg()
				this.swiperList = message
			},
			async getNavList() {
				const {
					data: {
						message,
						meta
					}
				} = await uni.$http.get('/api/public/v1/home/catitems')
				if (meta.status !== 200) return uni.$showMsg()
				this.navList = message
			},
			navClickHandler(item) {
				if (item.name === '分类') {
					uni.switchTab({
						url: '/pages/cate/cate'
					})
				}
			},
			async getFloorList() {
				const {
					data: {
						message,
						meta
					}
				} = await uni.$http.get('/api/public/v1/home/floordata')
				if (meta.status !== 200) return uni.$showMsg()
				// 改造数据，获取数据query跳转的参数
				// console.log(message)
				// this.floorList = message
				this.floorList = message.map(item => {
					return {
						floor_title: item.floor_title,
						product_list: item.product_list.map(e => {
							return {
								...e,
								navigator_url: e.navigator_url.split('?')[1]
							}
						})
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	// swiper
	.swiper {
		height: 330rpx;

		.swiper-item,
		image {
			width: 100%;
			height: 100%;
		}
	}

	// nav
	.nav-list {
		display: flex;
		justify-content: space-around;

		.nav-item {
			margin: 15px 0;

			image {
				width: 128rpx;
				height: 140rpx;
			}
		}
	}

	// 楼层
	.floor-list {
		width: 100%;

		.floor-item {
			width: 100%;

			.floor-title {
				width: 100%;
				height: 60rpx;
			}

			.floor-img-box {
				display: flex;
				padding-left: 10rpx;

				.right-img-box {
					display: flex;
					flex-wrap: wrap;
					justify-content: space-around;
				}
			}
		}
	}
	
	.search-box {
	  // 设置定位效果为“吸顶”
	  position: sticky;
	  // 吸顶的“位置”
	  top: 0;
	  // 提高层级，防止被轮播图覆盖
	  z-index: 999;
	}
</style>
